/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-4-23 上午9:41 Created by dwzhang
 */
package com.jade.framework.base.context;

import java.util.Locale;
import javax.annotation.PostConstruct;
import javax.inject.Named;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.NoSuchMessageException;

/**
 * 采用Spring作为对象容器时的上下文实现。
 *
 * @author dwzhang
 * @version 1.0 13-4-23 上午9:41
 */
@Named ("system_applictionContext")
public class SpringApplicationContext implements JadeApplicationContext, ApplicationContextAware
{
    private ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException
    {
        this.applicationContext = applicationContext;
    }

    public ApplicationContext getApplicationContext()
    {
        return applicationContext;
    }

    @PostConstruct
    public void init()
    {
        ApplicationContextUtils.setApplicationContext(this);
    }

    public <T> T getBean(String name)
    {
        return (T)applicationContext.getBean(name);
    }

    public <T> T getBean(String name, Class<T> tClass)
    {
        return (T)applicationContext.getBean(name, tClass);
    }

    public <T> T getBean(Class<T> tClass)
    {
        return (T)applicationContext.getBean(tClass);
    }

    public boolean containsBean(String name)
    {
        return applicationContext.containsBean(name);
    }

    public String getMessage(String code, Locale loc, Object... args)
    {
        try {
            return applicationContext.getMessage(code, args, loc);
        }
        catch (NoSuchMessageException t) {
            return null;
        }
    }
}
